/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao


import entity.Address;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author JAVIER
 */
class AddresessDAO  implements DAO <Address> {
    
    private Integer ID = 0;
        
    private static final String SQL_INSERT = "INSERT INTO Addresses ( clientid, address, type) VALUES ( ?, ?, ?);";
    
    private static final String SQL_SELECT = "SELECT * FROM Addresses where id= ?";
    
    private static final String SQL_SELECT_PROPERTY = " SELECT * FROM Addresses where ? = ?" ;
    
    private static final String SQL_SELECT_All = " SELECT * FROM Addresses";
    
    private static final String SQL_UPDATE = " UPDATE Addresses SET id=?, clientid=?, address=?, type=? WHERE id = ?";

    private static final String SQL_DELETE = " DELETE FROM Addresses WHERE id = ?";
    
    Integer create(Address adrs, Connection connect) throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_INSERT,Statement.RETURN_GENERATED_KEYS);
            //ps.setInt(1, Client.getId());
            ps.setInt(1, adrs.getClient().getId());
            ps.setString(2, adrs.getAddress());
            ps.setString(3,adrs.getType());

            ID = ps.executeUpdate();
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Boolean remove(Address adrs, Connection connect) throws SQLException {
        Boolean result= false;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_DELETE);
            ps.setInt(1, adrs.getId());
            ps.executeUpdate();
            result = true;
        } finally {
            close(ps);
        }
        
        return result;
        //TODO implement
    }
    
    Boolean update(Address adrs, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_UPDATE,Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, adrs.getId());
            ps.setInt(2, adrs.getClient().getId());
            ps.setString(3, adrs.getAddress());
            ps.setString(4,adrs.getType());
            ps.setInt(5, adrs.getId());
            ID = ps.executeUpdate();
            
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Address find(Integer id, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT);
            ps.setString(1, id);
            rs = ps.executeQuery();
            Address Addr = new Address();
            result = null;
            Addr = get_address(rs);
            return Addr;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List findAll(Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Address Addr = new Address();
            result = null;
            while (rs.next()) {
                Addr = get_Address(rs);
                results.add(Addr);}
            return result;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List select_by_property(String camp, String Value, Connection connect)throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_PROPERTY);
            ps.setString(1, camp);
            ps.setString(2, value);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Address Addr = new Address();
            result = null;
            while (rs.next()) {
                Addr = get_Address(rs);
                results.add(Addr);}
            return result;

        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
        
    Address get_Address (ResultSet rs) throws SQLException {
 
        Address Addr = new Address();
        Addr.setId(rs.getInt("Id"));
        Addr.setClientId(rs.getString("ClientId"));
        Addr.setAddress(rs.getString("Address"));
        Addr.setType(rs.getString("Type"));

        return Addr;
    }
    
}
